package com.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.mall.common.utils.Response;
import com.mall.common.utils.ResponseMessage;
import com.mall.vo.AttrsVo;
import com.mall.vo.PurchaseVo;
import com.mall.vo.SkuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.entity.SkuInfo;
import com.mall.service.SkuInfoService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * sku信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-08-24 16:47:36
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;



    @GetMapping("/buy/{spuId}")
    public Response getBuy(@RequestBody Long[]attrIds,@PathVariable("spuId") Long spuId){
        // 返回价格 图片信息 副标题
        PurchaseVo purchaseVo = skuInfoService.selectPriceAndStock(attrIds, spuId);
        List<SkuInfo>list=skuInfoService.selectPrice(attrIds,spuId);
        if (list == null){
            return new Response(ResponseMessage.FAIL);
        }
        return new Response(ResponseMessage.SUCCESS).data(purchaseVo);
    }




    /**
     * 列表
     */
    @RequestMapping("/listAll")
    public R listAll(){
        List<SkuInfo> list = skuInfoService.list(null);
        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    //  @RequiresPermissions("product:skuinfo:info")
    public R info(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfo skuInfo){
        skuInfoService.save(skuInfo);

        return R.ok();
    }


    @PostMapping("/save/info")
    public R saveinfo(@RequestBody SkuVo vos){
//        System.out.println(vos);
        skuInfoService.saveSku(vos);
        return R.ok();
    }

    @PostMapping("/add/{id}/{groupName}")
    public R add(@PathVariable("groupName") String groupName,@PathVariable("id") Long id,@RequestBody AttrsVo[] attrsVos){
        skuInfoService.saveAllAttr(groupName,id,attrsVos);
        return new R();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfo skuInfo){
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
